package com.winning.java.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * @author: rqx
 * @create: 2023/6/5 11:38
 * @description:
 */
@Aspect
@Component
@Slf4j
public class AopClass {

    public AopClass() {
        log.info("AOP Init");
    }

    @Pointcut(value = "execution(* com.winning.java.aop.BaseClass.call2(..))")
    private void cutPoint2() {
    }

    @Around("cutPoint2()")
    public void aop2(ProceedingJoinPoint point) throws Throwable {
        log.info("");
        log.info("before cutPoint2");
        point.proceed();
        log.info("end cutPoint2");
    }

    @Around(value = "@annotation(MyAnotaion)")
    public void aop3(ProceedingJoinPoint point) throws Throwable {
        log.info("");
        log.info("before cutPoint3");
        point.proceed();
        log.info("end cutPoint3");
    }

    @Around(value = "execution(* *.call4())")
    public void aop4(ProceedingJoinPoint point) throws Throwable {
        log.info("");
        log.info("before cutPoint4");
        var sw = new StopWatch();
        sw.start();
        try {
            point.proceed();
        } catch (Exception ex) {
            log.info("find error! ex: {}", ex.getMessage());
        }
        sw.stop();
        log.info("end cutPoint4");
        log.info("use time : {}",sw.getTotalTimeMillis());
    }
}
